package com.dididoctor.patient.Activity.Consult;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.Activity.Message.SystemMessage.SystemMessage;
import com.dididoctor.patient.Bean.Token;
import com.dididoctor.patient.Http.MyHttpResponseHandler;
import com.dididoctor.patient.MV.BasePresenter;
import com.dididoctor.patient.MV.BusinessClient;
import com.dididoctor.patient.MV.Response;
import com.dididoctor.patient.Utils.Util;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： xmppPresenter
 * @包名称： com.dididoctor.patient.Activity.Consult
 * @创建人： zhanjunjie
 * @创建时间：2016/4/7 11:05
 * @修改人：
 * @修改时间：2016/4/7 11:05
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class ConsultPresenter extends BasePresenter {

    private ConsultView view;

    public ConsultPresenter(Context context, ConsultView view) {
        super(context, view);
        this.view = view;
    }


    public void getdoctorInfo() {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        BusinessClient.post(ConstantValue.signDoctor, params, new MyHttpResponseHandler() {
            private String hasDoctor = "";//是否有签约医生1有0没有

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    Token.setHasDoctor(response.getString("hasDoctor"));
                    if ("0".equals(response.getString("hasDoctor"))) {
                        view.getdoctorno();
                    } else if ("1".equals(response.getString("hasDoctor"))) {
                        Consult doctor = new Consult();
                        Map<String, Object> doctormap = response.getMap("doctor");
                        doctor.setOnline(Util.toString(doctormap.get("online")));
                        doctor.setCallSwitch(Util.toString(doctormap.get("callSwitch")));
                        doctor.setDoctorId(Util.toString(doctormap.get("doctorId")));
                        doctor.setHospitalId(Util.toString(doctormap.get("hospitalId")));
                        doctor.setHeadPic(Util.toString(doctormap.get("headPic")));
                        doctor.setName(Util.toString(doctormap.get("name")));
                        doctor.setHospital(Util.toString(doctormap.get("hospital")));
                        doctor.setEvaluate(Util.toString(doctormap.get("evaluate")));
                        doctor.setSignNum(Util.toString(doctormap.get("signNum")));
                        doctor.setPosition(Util.toString(doctormap.get("position")));
                        doctor.setPrice(Util.toString(doctormap.get("price")));
                        doctor.setPhone(Util.toString(doctormap.get("phone")));
                        doctor.setAccId(Util.toString(doctormap.get("accId")));
                        view.getdoctorsucced(doctor);
                    }

                } else {
                    view.showToastMessage(response.getMsg());
                    view.getdoctorfail();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getdoctorfail();
            }
        });
    }

    public void getgoAndFrom(String hosId) {
//        hosId	String	是	医生所属医院id
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("hosId", hosId);
        BusinessClient.post(ConstantValue.medicalchatUrl, params, new MyHttpResponseHandler() {
            private String hasDoctor = "";//是否有签约医生1有0没有


            @Override
            public void onMyResponse(Response response) {
                if ("1".equals(response.getStatus())) {
                    String isCmp = response.getString("isCmp");
                    String recId = response.getString("recId");
                    view.IntentP2PActivity(recId, "4");
                } else if ("2".equals(response.getStatus())) {
                    String recId = response.getString("recId");
                    view.IntentP2PActivity(recId, "2");
                } else if ("3".equals(response.getStatus())) {
                    String recId = response.getString("recId");
                    view.IntentP2PActivity(recId, "3");
                } else if ("4".equals(response.getStatus())) {
                    String recId = response.getString("recId");
                    view.IntentP2PActivity(recId, "4");
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getdoctorfail();
            }
        });
    }

    public void getCalling(String callee, String hospitalId, String doctorId, String recId, String disid) {
//                        userId	String	是	用户id
//                        token	String	是	唯一标示
//                        reqType	String	是	1-医生拨打患者电话 2-患者拨打医生电话
//                        accId	String	是	云信id
//                        caller	String	是	主叫电话号码
//                        callee	String	是	被叫电话号码
//                        hosId	String	否	医生医院id reqType=2时用
//                        doctorId	String	否	医生id reqType=2时用
//                        diseId	String	否	病种id reqType=2时用
//                        recId	String	否	病历id reqType=1时用
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("reqType", "2");
        params.put("accId", Token.getAccId());
        params.put("caller", Token.getPhone());
        params.put("callee", callee);
        params.put("hosId", hospitalId);
        params.put("doctorId", doctorId);
        params.put("diseId", disid);
        params.put("recId", recId);
        BusinessClient.post(ConstantValue.callingUrl, params, new MyHttpResponseHandler() {
            private String hasDoctor = "";//是否有签约医生1有0没有

            @Override
            public void onMyResponse(Response response) {
                String status = response.getStatus();
                if ("0".equals(status)) {
                    view.showToastMessage("拨打失败");
                } else if ("1".equals(status)) {
                    view.showToastMessage("拨打成功");
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getdoctorfail();
            }
        });
    }

    void getmessageInfo(int pageIndex) {
        RequestParams params = new RequestParams();
        params.put("pageIndex", pageIndex);
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("pageSize", "10");
        BusinessClient.post(ConstantValue.message, params, new MyHttpResponseHandler() {

            private List<SystemMessage> messages = new ArrayList<SystemMessage>();
            private List<Map<String, String>> maps = new ArrayList<Map<String, String>>();

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    maps = response.getListData("messageList");
                    for (Map<String, String> map : maps) {
                        SystemMessage message = new SystemMessage();
                        String contents = Util.toString(map.get("contents"));
                        if (contents.indexOf("type") !=-1){
                            JSONObject object = JSON.parseObject(contents);
                            String  type = object.getString("type");
                            message.setType(type);
                            if ("1".equals(type)) {
//                                    内容:活动内容(如:迎五一，一毛钱签约医生活动开始了，赶快去签约吧)
//                                    点击:活动详情界面

                                String webUrl = object.getString("webUrl");
                                message.setWebUrl(webUrl);
                                String picUrl = object.getString("picUrl");
                            }else  if ("2".equals(type)){
//                                    内容:您于4月21日 18:24分咨询的问题，医生已经为您下达诊疗建议，可以到个人中心我的病历中查看
//                                    点击:病历详情页
                                String recType =object.getString("recType");
                                String recId = object.getString("recId");
                                message.setRecType(recType);
                                message.setRecId(recId);
                            } else  if ("4".equals(type)){
//                                  内容:您于4月21日 18:24分预约电话服务已经结束，医生已经对您下达诊疗建议，快去看看吧
//                                    点击:跳转病历详情页面
                                String recType =object.getString("recType");
                                String recId = object.getString("recId");
                                message.setRecType(recType);
                                message.setRecId(recId);
                            }else if ("9".equals(type)){
                                //为了统一内容本地将title和contents做了互换
                                String des = object.getString("content").toString();
                                message.setTitle(des);
                            }
                            String des = object.getString("msg");
                            message.setContents(des);
                        }else{
                            message.setMessageId(Util.toString(map.get("messageId")));
                            message.setTitle(Util.toString(map.get("title")));
                            message.setContents(Util.toString(map.get("contents")));
                            message.setWebUrl(Util.toString(map.get("webUrl")));
                            message.setProdId(Util.toString(map.get("prodId")));
                        }

                        messages.add(message);
                    }
                    view.getmessage(messages);
                } else {
                    view.getmessage(messages);
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getmessagefail();
            }
        });
    }

}
